package day11.task;


import java.util.*;

// 测试员工
class TestEmployee {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 用来存储员工的信息
        List<Employee> list = new ArrayList<>();
        while (true) {
            String input = scanner.nextLine();// next()读不到 ----> nextLine()它能读到空格
            // 当输入end字符串文本结束循环
            if ("end".equals(input)) {
                break;
            }
            // split(String regex) 分割字符串
            String[] names = input.split(" ");
            // 员工的姓名需要存入到list当中
            for (String name : names) {
                // 添加开关
                boolean flag = true;
                for (int i =0;i<list.size();i++) {
                    if (name.equals(list.get(i).getName())) {
                        //list.get(i).setCount(list.get(i).getCount()+1);
                        //list.get(i).getCount()+1
                        list.get(i).setCount();
                        flag = false;
                        break;
                    }
                }
                // 判断flag值是否为true
                if (flag) {
                    list.add(new Employee(name,1));
                }
            }
        }
        list.sort(new Comparator<Employee>() {//第一种
            @Override
            public int compare(Employee o1, Employee o2) {
                return o2.getCount() - o1.getCount();
            }
        });
        list.sort(Comparator.comparingInt(Employee::getCount).reversed());//第二种
        list.sort((a,b)->{//第三种
//            if(a.getCount().equals(b.getCount())){
//                return 0;
//            }
                return b.getCount().compareTo(a.getCount());

        });
        // 经过这个sort方法之后这个list就降序了
        for (Employee employee : list) {
            System.out.println(employee);
        }

    }
}

// 定义一个员工类
class Employee {

    // 员工类中有两个属性  姓名  次数
    private String name;
    private Integer count;

    // 重写equal()和hashCode()方法  判定员工姓名相同
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public String toString() {
        return  name + " "+ count ;// 员工姓名 出勤次数
    }

    public Employee() {
    }

    public Employee(String name) {
        this.name = name;
    }

    public Employee(String name, Integer count) {
        this.name = name;
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getCount() {
        return count;
    }

    /*public void setCount(Integer count) {
        this.count = count;
    }*/

    public void setCount() {
        count++;
    }
}
